/**
 * \* Created with IntelliJ IDEA.
 * \* User: 冯若航
 * \* Date: 2021/5/18
 * \* Time: 17:06
 * \* To change this template use File | Settings | File Templates.
 * \* Description:
 * \
 */
public class 删除链表的倒数第N个结点 {
    public class ListNode{
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    public ListNode removeNthFromEnd(ListNode head, int n) {
        //虚拟头结点
        ListNode pre=new ListNode(0);
        pre.next=head;
        ListNode quick=pre,slow=pre;
        //这样使得满指针最后指向倒数第n个节点的前一个节点，方便删除
        while (n!=0){
            quick=quick.next;
            n--;
        }
        while (quick.next!=null){
            quick=quick.next;
            slow=slow.next;
        }
        slow.next=slow.next.next;
        return pre.next;
    }
}